<template>
  <div class="login-container">
    <!--log部分-->
    <div class="logo">
      <img src="../assets/logo2.jpeg">
    </div>
    <el-form :model="costomer" :rules="rules" ref="userForm" class="loginform_style" label-width="0px">
      <el-form-item prop="tel">
        <el-input placeholder="请输入电话号码" prefix-icon="el-icon-user-solid" v-model="costomer.tel"></el-input>
      </el-form-item>
      <el-form-item prop="yzm">
        <el-row>
          <el-col :span="13">
            <el-input type="text" v-model="costomer.yzm" autocomplete="off" placeholder="请输入验证码" prefix-icon="el-icon-key"></el-input>
          </el-col>
          <el-col :span="10">
            <el-button type="success" :disabled="isdis" @click="getYZM()">{{this.btnMsg}}</el-button>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item class="btns">
        <el-button type="success" @click="submitForm('userForm')">提交</el-button>
        <el-button type="info" @click="resetForm('userForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
    export default {
        name: "Clogin",
      data(){
        return {
          costomer:{
            tel: "",
            yzm:''
          },
          isdis:false,
          btnMsg:"发送验证码",
          Totalnum:30,
          rules: {
            tel: [
              { required: true, message: '请输入电话号', trigger: 'blur' },
              {pattern:/^1[3|4|5|6|7|8|9][0-9]{9}$/, message: '电话号码不合法', trigger: 'blur' }
            ],
            yzm:[
              { required: true, message: '请输入验证码', trigger: 'blur' },
              { min: 4, message: '请输入4以上位数的验证码', trigger: 'blur' }
            ]
          }
        }
      },
      methods: {
        getYZM(){
          debugger
          //电话号验证用过才发送
          if(!(/^1[3|5|8|7|4][0-9]{9}$/).test(this.costomer.tel)){
            this.$message.error("号码格式错误，请重新输入")
            return;
          }
          this.$http.post("/customer/sendYZM/"+this.costomer.tel).then((ref)=>{
            debugger
            if (ref.data.success) {
              this.$message({
                message:ref.data.message,
                type:"success"
              })
            } else {
              this.$message.error(ref.data.message)
            }
          })
          let set=window.setInterval(()=>{
            this.isdis=true;
            let time=this.Totalnum--;
            this.btnMsg=time+"秒后重新发送";
            if(time<=0){
              window.clearInterval(set);//清除
              this.Totalnum=30;
              this.btnMsg="发送验证码";
              this.isdis=false;
            }
          },1000)
        },
        submitForm() {
          this.$refs['userForm'].validate((valid) => {
            if (!valid) {
              return ;
            }
            debugger
            this.$http.post("/customer/clogin",this.costomer).then((ref)=>{
              if (ref.data.success) {
                debugger;
                this.$message({
                  message: ref.data.message,
                  type: "success"
                })
                window.sessionStorage.setItem("costomer",JSON.stringify(ref.data.result.costomer));
                window.sessionStorage.setItem("linkman",JSON.stringify(ref.data.result.linkman));
                window.sessionStorage.setItem("token",ref.data.result.token)
                //跳转到主界面
                this.$router.push("/customerHome")
              } else {
                this.$message.error(ref.data.message)
              }
            })
          });
        },
        resetForm(formName) {
          this.$refs['userForm'].resetFields();
        }
      }
    }
</script>

<style lang="less" scoped>
  .login-container {
    width: 450px;
    height: 314px;
    background-color: #fff;
    position: absolute;
    top: 25%;
    left: 50%;
    transform: translateX(-50%);
    border-radius: 5px;

    .logo {
      width: 130px;
      height: 130px;
      border: 1px solid #eee;
      border-radius: 50%;
      padding: 8px;
      box-shadow: 0 0 10px #eee;
      position: absolute;
      left: 50%;
      transform: translateX(-50%) translateY(-60px);
      background-color: #fff;

      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eee;
      }
    }

    .loginform_style {
      padding: 0 20px;
      margin-top: 110px;
    }

    .btns {
      display: flex;
      justify-content: center;
    }
  }

</style>

